
package simuri.uridb;


import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import simuri.controller.GenericController;

/**
 * implements the Graphical User Interface of the disambiguation framework.
 * @author fomy
 */

public class GUI {
  
 static String algo ;
    
  public static void main (String[]args){
          
    final JFrame f = new JFrame("NED-Framework");
    Dimension d = new Dimension(600,600);
    f.setSize(d);
    f.setResizable(false);
    JPanel upperPanel = new JPanel(new BorderLayout());
    JPanel lowerPanel = new JPanel(new BorderLayout());
    JPanel centerPanel = new JPanel(new BorderLayout());
    f.getContentPane().add(upperPanel,"North");
    f.getContentPane().add(centerPanel,"East");
    f.getContentPane().add(lowerPanel, "South");
    
   //Input Panel
   final JTextArea jt = new JTextArea();
    jt.setLineWrap(true);
    jt.setWrapStyleWord(true);
    jt.setEditable(true);
    JScrollPane scr = new JScrollPane(jt);
    scr.setPreferredSize(new Dimension(590,200)); 
    scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    upperPanel.setBorder(BorderFactory.createTitledBorder("Input"));
    upperPanel.add(scr,BorderLayout.CENTER);
    
    //output Panel
    final JTextPane sf = new JTextPane();
    sf.setContentType("text/html");
    
    sf.setEditable(false);
    sf.addHyperlinkListener(new HyperlinkListener(){
        public void hyperlinkUpdate(HyperlinkEvent e){
         if(e.getEventType()== HyperlinkEvent.EventType.ACTIVATED){
        
          if( Desktop.isDesktopSupported()){
            try {
                try {
                    Desktop.getDesktop().browse(e.getURL().toURI());
                } catch (IOException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (URISyntaxException ex) {
                Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
         }
        }
    });
    
    JScrollPane sc = new JScrollPane(sf);
    sc.setPreferredSize(new Dimension(590,200));
    sc.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    lowerPanel.setBorder(BorderFactory.createTitledBorder("Output"));
    lowerPanel.add(sc,BorderLayout.CENTER);
    
    //creating Menubar
     JMenuBar menubar = new JMenuBar();
    
 // adding File menu 
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    JMenuItem fileOpen = new JMenuItem("Open");
    fileOpen.setMnemonic(KeyEvent.VK_O);
    fileOpen.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){ 
    try
       {
        JFileChooser jfc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Only Text File", "txt");
        jfc.setFileFilter(filter);
        FileReader reader = null;

        if (jfc.showOpenDialog(f)==JFileChooser.APPROVE_OPTION)
        {
        File selectedFile = jfc.getSelectedFile();
        if (selectedFile.canRead() && selectedFile.exists())
        {
         String filepath = selectedFile.getAbsolutePath();
        reader = new FileReader(selectedFile);
        }
        }
    BufferedReader in = new BufferedReader(reader);
    String inputLine = in.readLine();
    if(jt.getText() != null) jt.setText("");
    while(inputLine!=null)
    {
      jt.append(inputLine);
      inputLine = in.readLine();
    }
    in.close();
  }catch(IOException ex)
  {
  
  }catch(NullPointerException ex)
  {
  }
           }  
        });
        
    JMenuItem fileExit = new JMenuItem("Exit");
    fileExit.setMnemonic(KeyEvent.VK_C);
    fileExit.setToolTipText("Exit application");
    fileExit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    file.add(fileOpen);
    file.addSeparator();
    file.add(fileExit);
        
    // adding Disambiguierung Algorithms Menu
     JMenu algorithm = new JMenu("Disambiguation Algorithms");
     JRadioButton apriori = new JRadioButton("apriori Algorithm");
     algorithm.add(apriori);
     JRadioButton linear = new JRadioButton("linear Algorithm");
     algorithm.add(linear);
   
     final JRadioButton similarity = new JRadioButton("similarity Algorithm");
     algorithm.add(similarity);
     final JRadioButton weightedcoherence = new JRadioButton("weightedcoherence Algorithm");
     algorithm.add(weightedcoherence);
     
      ActionListener algoActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
      AbstractButton aButton = (AbstractButton) actionEvent.getSource();
      algo = aButton.getText();
      System.out.println("Selected: " + aButton.getText());
      }
    };
      
     //Group the radio buttons.
     ButtonGroup group = new ButtonGroup();
     group.add(linear);
     //group.add(normalized);
     group.add(similarity);
     group.add(weightedcoherence);
     group.add(apriori);
     
    linear.addActionListener(algoActionListener);
    //normalized.addActionListener(algoActionListener);
    similarity.addActionListener(algoActionListener);
    weightedcoherence.addActionListener(algoActionListener);
    apriori.addActionListener(algoActionListener);
     
     menubar.add(file);
     menubar.add(algorithm);    
     f.setJMenuBar(menubar);
        
  // submit button
   JButton button = new JButton("Submit");
   button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        if(jt.getText() != null){ 
        sf.setText("<html><body><p>"+GenericController.annotateCorpus1(jt.getText(),algo)+"</p></body></html>");
        }else{}
         }  
       }); 
    centerPanel.add(button);
    
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
  }
   
}
